package com.example.app.utils;

import com.example.app.constants.CommonConstants;
import com.example.app.model.User;
import com.example.app.model.common.CommonModel;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by wangtao on 2018/3/13.
 */
public class BusinessUtil {

    /**
     * 公共属性绑定
     * @param model
     */
    public static void appendRecord(CommonModel model) {
        User user = RequestUtil.getLoginUser();
        if(model.getCreationTime() != null){
            if(user != null){
                model.setUpdatedBy(user.getUsername());
            }
            model.setUpdateTime(new Date());
            model.setUpdateMethod("system");
        }else{
            if(user != null){
                model.setCreatedBy(user.getUsername());
            }
            model.setCreationTime(new Date());
            model.setCreationMethod("system");
            model.setUpdateMethod(null);
            model.setUpdateTime(null);
            model.setUpdatedBy(null);
            model.setDeleteFlag(0);
        }
    }

    /**
     * 属性绑定
     * @param obj
     * @param source
     * @param clazz
     */
    public static void convertValue(Object obj, Object source, Class clazz){
        ObjectMapper mapper = new ObjectMapper();
        obj = mapper.convertValue(source, clazz);
    }

    /**
     * 获取本周的第一天
     * @param date
     * @return
     */
    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    /**
     * 获取当前时间所在周的第一天
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getFirstOfWeek(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(DateUtils.parseDate(date, CommonConstants.DATE_FORMATS));
        int d = 0;
        if(cal.get(Calendar.DAY_OF_WEEK)==1){
            d = -6;
        }else{
            d = 2-cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        //所在周开始日期
        return sdf.format(cal.getTime()) + " 00:00:00";
    }

    /**
     * 获取当前时间所在周的最后一天
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getLastOfWeek(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(DateUtils.parseDate(date, CommonConstants.DATE_FORMATS));
        int d = 0;
        if(cal.get(Calendar.DAY_OF_WEEK)==1){
            d = -6;
        }else{
            d = 2-cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        cal.add(Calendar.DAY_OF_WEEK, 6);
        //所在周开始日期
        return sdf.format(cal.getTime()) + " 23:59:59";
    }
}
